package cn.pegasus.teamwork.pegasus.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.iyizhan.teamwork.base.service.impl.BaseServiceImpl;
import cn.iyizhan.teamwork.base.util.PageObject;
import cn.pegasus.teamwork.pegasus.entity.BussWasherFundstatistics;
import cn.pegasus.teamwork.pegasus.entity.BussWasherInfo;
import cn.pegasus.teamwork.pegasus.mapper.BussWasherInfoMapper;
import cn.pegasus.teamwork.pegasus.service.BussUcenterAreaService;
import cn.pegasus.teamwork.pegasus.service.BussWasherFundstatisticsService;
import cn.pegasus.teamwork.pegasus.service.BussWasherInfoService;
import cn.pegasus.teamwork.sysconfig.service.SysConfigSmsService;

/**
 * @author ryan.bian
 * @version 
 */
@Service("washerInfoService")
public class BussWasherInfoServiceImpl extends BaseServiceImpl<BussWasherInfo, BussWasherInfoMapper> implements BussWasherInfoService {
	// 注入当前dao对象
    @Autowired
    private BussWasherInfoMapper bussWasherInfoMapper;
    @Autowired
	private BussUcenterAreaService ucenterAreaService;
    @Autowired
    private SysConfigSmsService sysConfigSmsService;
    
    @Autowired
    private BussWasherFundstatisticsService bussWasherFundstatisticsService;
    public BussWasherInfoServiceImpl() {
        setMapperClass(BussWasherInfoMapper.class, BussWasherInfo.class);
    }

	@Override
	public PageObject<BussWasherInfo> PageList(Map<String, Object> map) {
		map.put("sortName", "create_time");
        map.put("sortOrder", "desc");
        PageObject<BussWasherInfo> page = super.Pagequery(map);
        List<BussWasherInfo> list = page.getDataList();
        if(list != null && list.size() > 0){
        	for(BussWasherInfo info : list){
        		//查询押金
        		BussWasherFundstatistics washer = bussWasherFundstatisticsService.findDeposit(info.getId());
        		if(washer != null){
        			info.setWasherDeposit(washer.getWasherDeposit()*0.01);
        		}
        		//查询负责区域
        		if(null != info.getAreaId()){
        			String name = ucenterAreaService.getAreaName(info.getAreaId());
            		info.setAreaName(name);
        		}
        	}
        }
        page.setDataList(list);
		return page;
	}
		
	@Override
	public void updateStatus(Integer id, Integer washerStatus,
			Integer washerIsdepEntrust, Integer washerExamine,String washerMachinecode) {
		BussWasherInfo info = new BussWasherInfo();
		info.setId(id);
		if(null != washerStatus){
			info.setWasherStatus(washerStatus);
		}
		if(null != washerIsdepEntrust){
			info.setWasherIsdepEntrust(washerIsdepEntrust);
		}
		if(null != washerExamine){
			info.setWasherExamine(washerExamine);
		}
		if(null != washerMachinecode){
			info.setWasherMachinecode(washerMachinecode);
		}
		bussWasherInfoMapper.updateByEntity(info);
		//短信通知
		if(null != washerExamine){
			BussWasherInfo infos = this.findWasherInfo(id);
			if(washerExamine==1){ //通过
				sysConfigSmsService.sendWasherRegSHNotice(infos.getWasherMobile(), 1);
			}
			if(washerExamine==3){ //不通过
				sysConfigSmsService.sendWasherRegSHNotice(infos.getWasherMobile(), 2);
			}
		}
		if(null != washerIsdepEntrust){
			BussWasherInfo infos = this.findWasherInfo(id);
			if(washerIsdepEntrust==1){
				sysConfigSmsService.sendWasherRegYJSHNotice(infos.getWasherMobile());
			}
		}
	}

	@Override
	public BussWasherInfo findWasherInfo(Integer id) {
		BussWasherInfo info = new BussWasherInfo();
		info.setId(id);
		BussWasherInfo i = bussWasherInfoMapper.selectSingle(info);
		if(null != i.getAreaId()){
			String name = ucenterAreaService.getAreaName(i.getAreaId());
    		i.setAreaName(name);
		}
		return i;
	}

	@Override
	public void updateWasher(Map<String, Object> map) {
		BussWasherInfo info = M2O(map);
		info.setUpdateTime(new Date());
		
		if(map.get("rentBalance")!=null){
			Double balance = Double.valueOf(map.get("rentBalance").toString());
			long money = (long)(balance*100);
			info.setRentBalance(money);
		}
		
		bussWasherInfoMapper.updateByEntity(info);
	}

	@Override
	public void update(Map<String, Object> map) {
		BussWasherInfo info = M2O(map);
		info.setCreateTime(new Date());
		info.setUpdateTime(new Date());
		bussWasherInfoMapper.updateByEntity(info);;
	}
}
